﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Imports TemplateParser
Partial Class FrontEnd_Control_Users_Profile
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadType()
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            LoadDate()
            ShowData()
        End If
    End Sub
    Protected Sub LoadType()
        Using data As New VSEntities
            'Khu vuc
            ddlKhuVuc.AppendDataBoundItems = True
            ddlKhuVuc.Items.Insert(0, New ListItem("---Chon---", "0"))
            Dim ba = (From ca In data.DonViHanhChinhs Select ca.Id, ca.Title).ToList
            ddlKhuVuc.DataTextField = "Title"
            ddlKhuVuc.DataValueField = "ID"
            ddlKhuVuc.DataSource = ba
            ddlKhuVuc.DataBind()
        End Using
    End Sub

    Private Sub LoadDate()
        ddlDay.AppendDataBoundItems = True
        ddlDay.Items.Add(New ListItem("Ngày", "0"))
        ddlDay.DataSource = Enumerable.Range(1, 31)
        ddlDay.DataBind()
        ddlDay.SelectedValue = 0

        ddlMonth.AppendDataBoundItems = True
        ddlMonth.Items.Add(New ListItem("Tháng", "0"))
        ddlMonth.DataSource = Enumerable.Range(1, 12)
        ddlMonth.DataBind()
        ddlMonth.SelectedValue = 0

        ddlYear.AppendDataBoundItems = True
        ddlYear.Items.Add(New ListItem("Năm", "0"))
        'chi cho dang ky tu 1945 va < hien tai -10
        ddlYear.DataSource = Enumerable.Range(1945, Now.Year - (1945 + 10))
        ddlYear.DataBind()
        ddlYear.SelectedValue = 0
    End Sub
    Private Sub ShowData()
        Using Data As New VSEntities
            Dim intUserId As Integer = IIf(IsNothing(Session("UserID")), 0, Session("UserID"))
            Dim p As User = (From q In Data.Users Where q.UserId = intUserId Select q).FirstOrDefault
            If Not p Is Nothing Then
                txtFullName.Text = IIf(IsNothing(p.FullName), "", p.FullName)
                If Not p.DateOfBirth Is Nothing Then
                    ddlDay.SelectedValue = CDate(p.DateOfBirth).Day
                    ddlMonth.SelectedValue = CDate(p.DateOfBirth).Month
                    ddlYear.SelectedValue = CDate(p.DateOfBirth).Year
                End If
                ddlGender.SelectedValue = IIf(IsNothing(p.Gender), 0, p.Gender)
                txtDiaChi.Text = IIf(IsNothing(p.DiaChi), "", p.DiaChi)
                txtDienThoai.Text = IIf(IsNothing(p.Phone), "", p.Phone)
                txtDienThoaiKhac.Text = IIf(IsNothing(p.MobileOrther), "", p.MobileOrther)
                ddlKhuVuc.SelectedValue = p.TinhTP


            End If
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        Using data As New VSEntities
            Dim intUserId As Integer = IIf(IsNothing(Session("UserID")), 0, Session("UserID"))
            Dim p As VSModel.User = (From q In data.Users Where q.UserId = intUserId Select q).FirstOrDefault
            Try
                p.FullName = txtFullName.Text.Trim
                p.Gender = ddlGender.SelectedValue
                p.Phone = txtDienThoai.Text.Trim()
                p.MobileOrther = txtDienThoaiKhac.Text.Trim
                p.DiaChi = txtDiaChi.Text.Trim()
                p.TinhTP = ddlKhuVuc.SelectedValue
                p.DateOfBirth = StringToDate(ddlDay.SelectedValue.ToString & "/" & ddlMonth.SelectedValue.ToString & "/" & ddlYear.SelectedValue.ToString, "dd/mm/yyyy")

                data.SaveChanges()

                'send email thong bao
                'Dim templateVars As New Hashtable()
                'templateVars.Add("FirstName", txtFist.Text)
                'templateVars.Add("LastName", txtLastname.Text)
                'templateVars.Add("Login", txtUsername.Text)
                'templateVars.Add("Password", txtPassword.Text)
                'Dim parser As New Parser(Server.MapPath("~/FrontEnd/EmailTemplate/Registration.htm"), templateVars)

                'Sendmail_SMTP("abcd@pup.edu.vn", "abcd", "mail.pup.edu.vn", txtEmail.Text, "", "VStore dang ky tai khoan thanh cong", parser.Parse, "123456", 25, False)


                'Insert_App_Log("Insert Member:" & txtUsername.Text.Trim & "", Function_Name.User, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật thông tin cá nhân thành công.');window.location ='" + MainDomain + "my-oto.html';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Đăng ký thành viên thất bại hãy kiểm tra lại đường truyền hoặc liên hệ với quản lý.');", Me.Page, True)
            End Try
       
        End Using
    End Sub

#End Region

End Class
